package cn.sangedon.review.analysis.strategy.specific;

import cn.sangedon.review.analysis.constant.StrategyEnum;
import cn.sangedon.review.analysis.service.StockStasticsService;
import cn.sangedon.review.common.domain.analysis.StockStrategy;
import cn.sangedon.review.common.domain.analysis.StockStrategyResult;
import cn.sangedon.review.common.service.data.StockDayInfoService;
import cn.sangedon.review.common.service.data.TradingDateService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 下跌缩量红策略执行类，今日成交量比昨日低，且之前已经连续下跌至少三日以上
 */
@Slf4j
@Component
public class FallShrinkRedStrategy extends AbstractScheduleTradeStrategy {
    @DubboReference
    private StockDayInfoService stockDayInfoService;

    @DubboReference
    private TradingDateService tradingDateService;

    @Autowired
    private StockStasticsService stockStasticsService;

    @Override
    public void runJob(List<StockStrategy> strategyList) {
        strategyList.stream().filter(e -> e.getStrategyName().equals(StrategyEnum.FALL_SHRINK_RED.getName())).findFirst().ifPresent(e -> {
            addSchedule(e);
        });
    }

    @Override
    public void execute() {
        log.info("开始执行下跌缩量红策略");
        String tradeDate = stockDayInfoService.getLatestDate();
        execute(tradeDate);
    }

    /**
     * 获取某个交易日复合下跌缩量红策略的股票
     *
     * @param tradeDate 交易日
     */
    public void execute(String tradeDate) {
        String preTradeDate = tradingDateService.getTradeDateAgo(tradeDate, 1);
        List<StockStrategyResult> list = stockStasticsService.fallShrinkRed(preTradeDate, tradeDate);
        for (StockStrategyResult strategyResult : list) {
            strategyResult.setStrategyId(stockStrategy.getId());
            strategyResult.setStrategyName(stockStrategy.getStrategyName());
        }
        stockStrategyResultService.saveBatch(list);
        log.info("符合下跌缩量红要求的股票数量:{}", list.size());
    }
}
